{
  "nbformat_minor": 0,
  "nbformat": 4,
  "cells": [
    {
      "source": [
        "#### Copyright 2017 Google LLC."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "JndnmDMp66FL"
      }
    },
    {
      "source": [
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "hMqWDc_m6rUC",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        },
        "cellView": "both"
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " # \ud150\uc11c \ub9cc\ub4e4\uae30 \ubc0f \uc870\uc791"
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "P0bQXjp499sl"
      }
    },
    {
      "source": [
        " **\ud559\uc2b5 \ubaa9\ud45c:**\n",
        "  * \ud150\uc11c\ud50c\ub85c\uc6b0 `\ubcc0\uc218` \ucd08\uae30\ud654 \ubc0f \ud560\ub2f9\n",
        "  * \ud150\uc11c \ub9cc\ub4e4\uae30 \ubc0f \uc870\uc791\n",
        "  * \uc120\ud615\ub300\uc218\ud559\uc758 \ub367\uc148 \ubc0f \uacf1\uc148 \uc9c0\uc2dd \ub418\uc0b4\ub9ac\uae30(\uc774 \uc8fc\uc81c\uac00 \uc0dd\uc18c\ud55c \uacbd\uc6b0 \ud589\ub82c [\ub367\uc148](https://en.wikipedia.org/wiki/Matrix_addition) \ubc0f [\uacf1\uc148](https://en.wikipedia.org/wiki/Matrix_multiplication) \ucc38\uc870)\n",
        "  * \uae30\ubcf8 \ud150\uc11c\ud50c\ub85c\uc6b0 \uc218\ud559 \ubc0f \ubc30\uc5f4 \uc791\uc5c5\uc5d0 \uc775\uc219\ud574\uc9c0\uae30"
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "I3BCiWJiCGsv"
      }
    },
    {
      "source": [
        "from __future__ import print_function\n\n",
        "import tensorflow as tf"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "85evKRsOIC5a",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ## \ubca1\ud130 \ub367\uc148\n",
        "\n",
        "\ud150\uc11c\uc5d0\uc11c \uc5ec\ub7ec \uc77c\ubc18\uc801\uc778 \uc218\ud559 \uc5f0\uc0b0\uc744 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4([TF API](https://www.tensorflow.org/api_guides/python/math_ops)). \ub2e4\uc74c \ucf54\ub4dc\ub294\n",
        "\uac01\uae30 \uc815\ud655\ud788 6\uac1c \uc694\uc18c\ub97c \uac00\uc9c0\ub294 \ub450 \ubca1\ud130(1-D \ud150\uc11c)\ub97c \ub9cc\ub4e4\uace0 \uc870\uc791\ud569\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "PT1sorfH-DdQ"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default():\n",
        "  # Create a six-element vector (1-D tensor).\n",
        "  primes = tf.constant([2, 3, 5, 7, 11, 13], dtype=tf.int32)\n",
        "\n",
        "  # Create another six-element vector. Each element in the vector will be\n",
        "  # initialized to 1. The first argument is the shape of the tensor (more\n",
        "  # on shapes below).\n",
        "  ones = tf.ones([6], dtype=tf.int32)\n",
        "\n",
        "  # Add the two vectors. The resulting tensor is a six-element vector.\n",
        "  just_beyond_primes = tf.add(primes, ones)\n",
        "\n",
        "  # Create a session to run the default graph.\n",
        "  with tf.Session() as sess:\n",
        "    print(just_beyond_primes.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "ng37e6ur-GZo",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ### \ud150\uc11c \ud615\ud0dc\n",
        "\n",
        "\ud615\ud0dc\ub294 \ud150\uc11c\uc758 \ud06c\uae30\uc640 \ucc28\uc6d0 \uc218\ub97c \uacb0\uc815\ud558\ub294 \ub370 \uc0ac\uc6a9\ub429\ub2c8\ub2e4. \ud150\uc11c \ud615\ud0dc\ub294 `\ubaa9\ub85d`\uc73c\ub85c \ud45c\ud604\ud558\uba70, `i`\ubc88\uc9f8 \uc694\uc18c\ub294 `i` \ucc28\uc6d0\uc5d0\uc11c \ud06c\uae30\ub97c \ub098\ud0c0\ub0c5\ub2c8\ub2e4. \uadf8\ub9ac\uace0 \uc774 \ubaa9\ub85d\uc758 \uae38\uc774\ub294 \ud150\uc11c\uc758 \uc21c\uc704(\uc608: \ucc28\uc6d0 \uc218)\ub97c \ub098\ud0c0\ub0c5\ub2c8\ub2e4.\n",
        "\n",
        "\uc790\uc138\ud55c \uc815\ubcf4\ub294 [\ud150\uc11c\ud50c\ub85c\uc6b0 \ubb38\uc11c](https://www.tensorflow.org/programmers_guide/tensors#shape)\ub97c \ucc38\uc870\ud558\uc138\uc694.\n",
        "\n",
        "\uba87 \uac00\uc9c0 \uae30\ubcf8 \uc608:"
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "fVvaXzzMGZid"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default():\n",
        "  # A scalar (0-D tensor).\n",
        "  scalar = tf.zeros([])\n",
        "\n",
        "  # A vector with 3 elements.\n",
        "  vector = tf.zeros([3])\n",
        "\n",
        "  # A matrix with 2 rows and 3 columns.\n",
        "  matrix = tf.zeros([2, 3])\n",
        "\n",
        "  with tf.Session() as sess:\n",
        "    print('scalar has shape', scalar.get_shape(), 'and value:\\n', scalar.eval())\n",
        "    print('vector has shape', vector.get_shape(), 'and value:\\n', vector.eval())\n",
        "    print('matrix has shape', matrix.get_shape(), 'and value:\\n', matrix.eval())\n"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "PWzvJnIAH_cF",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ### \ube0c\ub85c\ub4dc\uce90\uc2a4\ud305\n",
        "\n",
        "\uc218\ud559\uc5d0\uc11c\ub294 \uac19\uc740 \ud615\ud0dc\uc758 \ud150\uc11c\uc5d0\uc11c \uc694\uc18c\uac04 \uc5f0\uc0b0(\uc608: *add* \ubc0f *equals*)\ub9cc \uc2e4\ud589\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ud558\uc9c0\ub9cc \ud150\uc11c\ud50c\ub85c\uc6b0\uc5d0\uc11c\ub294 \ud150\uc11c\uc5d0\uc11c \uae30\uc874\uc5d0\ub294 \ud638\ud658\ub418\uc9c0 \uc54a\uc558\ub358 \uc5f0\uc0b0\uc744 \uc2e4\ud589\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ud150\uc11c\ud50c\ub85c\uc6b0\ub294 \uc694\uc18c\uac04 \uc5f0\uc0b0\uc5d0\uc11c \ub354 \uc791\uc740 \ubc30\uc5f4\uc744 \ud655\uc7a5\ud558\uc5ec \ub354 \ud070 \ubc30\uc5f4\uacfc \uac19\uc740 \ud615\ud0dc\ub97c \uac00\uc9c0\uac8c \ud558\ub294 **\ube0c\ub85c\ub4dc\uce90\uc2a4\ud305**(Numpy\uc5d0\uc11c \ucc28\uc6a9\ud55c \uac1c\ub150)\uc744 \uc9c0\uc6d0\ud569\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4 \ube0c\ub85c\ub4dc\uce90\uc2a4\ud305\uc744 \ud1b5\ud574 \ub2e4\uc74c\uacfc \uac19\uc740 \uacb0\uacfc\ub97c \uc5bb\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "* \ud53c\uc5f0\uc0b0\uc790\uc5d0 \ud06c\uae30\uac00 `[6]`\uc778 \ud150\uc11c\uac00 \ud544\uc694\ud55c \uacbd\uc6b0 \ud06c\uae30\uac00 `[1]` \ub610\ub294 \ud06c\uae30\uac00 `[]`\uc778 \ud150\uc11c\uac00 \ud53c\uc5f0\uc0b0\uc790\uac00 \ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "* \uc5f0\uc0b0\uc5d0 \ud06c\uae30\uac00 `[4, 6]`\uc778 \ud150\uc11c\uac00 \ud544\uc694\ud55c \uacbd\uc6b0 \ub2e4\uc74c \ud06c\uae30\uc758 \ud150\uc11c\uac00 \ud53c\uc5f0\uc0b0\uc790\uac00 \ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "  * `[1, 6]`\n",
        "  * `[6]`\n",
        "  * `[]`\n",
        "* \uc5f0\uc0b0\uc5d0 \ud06c\uae30\uac00 `[3, 5, 6]`\uc778 \ud150\uc11c\uac00 \ud544\uc694\ud55c \uacbd\uc6b0 \ub2e4\uc74c \ud06c\uae30\uc758 \ud150\uc11c\uac00 \ud53c\uc5f0\uc0b0\uc790\uac00 \ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "  * `[1, 5, 6]`\n",
        "  * `[3, 1, 6]`\n",
        "  * `[3, 5, 1]`\n",
        "  * `[1, 1, 1]`\n",
        "  * `[5, 6]`\n",
        "  * `[1, 6]`\n",
        "  * `[6]`\n",
        "  * `[1]`\n",
        "  * `[]`\n",
        "  \n",
        "**\ucc38\uace0:** \ud150\uc11c\uac00 \ube0c\ub85c\ub4dc\uce90\uc2a4\ud305\ub418\uba74 \ud150\uc11c\uc758 \ud56d\ubaa9\uc740 \uac1c\ub150\uc801\uc73c\ub85c **\ubcf5\uc0ac**\ub429\ub2c8\ub2e4. (\uc131\ub2a5\uc0c1\uc758 \uc774\uc720\ub85c \uc2e4\uc81c\ub85c \ubcf5\uc0ac\ub418\uc9c0\ub294 \uc54a\uc74c. \ube0c\ub85c\ub4dc\uce90\uc2a4\ud305\uc740 \uc131\ub2a5 \ucd5c\uc801\ud654\ub97c \uc704\ud574 \uac1c\ubc1c\ub428.)\n",
        "\n",
        "\uc804\uccb4 \ube0c\ub85c\ub4dc\uce90\uc2a4\ud305 \uaddc\uce59 \uc138\ud2b8\ub294 [Numpy \ube0c\ub85c\ub4dc\uce90\uc2a4\ud305 \ubb38\uc11c](http://docs.scipy.org/doc/numpy-1.10.1/user/basics.broadcasting.html)\uc5d0 \uc774\ud574\ud558\uae30 \uc27d\uac8c \uc798 \uc124\uba85\ub418\uc5b4 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\ub2e4\uc74c \ucf54\ub4dc\ub294 \uc55e\uc11c \uc124\uba85\ud55c \ud150\uc11c \ub367\uc148\uc744 \uc2e4\ud589\ud558\uc9c0\ub9cc \ube0c\ub85c\ub4dc\uce90\uc2a4\ud305\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "musamrLavR5S"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default():\n",
        "  # Create a six-element vector (1-D tensor).\n",
        "  primes = tf.constant([2, 3, 5, 7, 11, 13], dtype=tf.int32)\n",
        "\n",
        "  # Create a constant scalar with value 1.\n",
        "  ones = tf.constant(1, dtype=tf.int32)\n",
        "\n",
        "  # Add the two tensors. The resulting tensor is a six-element vector.\n",
        "  just_beyond_primes = tf.add(primes, ones)\n",
        "\n",
        "  with tf.Session() as sess:\n",
        "    print(just_beyond_primes.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "7lys_BeLy2SD",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ## \ud589\ub82c \uacf1\uc148\n",
        "\n",
        "\uc120\ud615\ub300\uc218\ud559\uc5d0\uc11c \ub450 \uac1c\uc758 \ud589\ub82c\uc744 \uacf1\ud560 \ub54c\ub294 \uccab \ubc88\uc9f8 \ud589\ub82c\uc758 *\uc5f4* \uc218\uac00 \ub450 \ubc88\uc9f8\n",
        "\ud589\ub82c\uc758 *\ud589* \uc218\uc640 \uac19\uc544\uc57c \ud588\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "-  `3x4` \ud589\ub82c\uacfc `4x2` \ud589\ub82c\uc744 \uacf1\ud558\ub294 \uac83\uc740 **_\uc720\ud6a8\ud569\ub2c8\ub2e4_**. \uc774\ub807\uac8c \ud558\uba74 `3x2` \ud589\ub82c\uc744 \uc5bb\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "- `4x2` \ud589\ub82c\uacfc `3x4` \ud589\ub82c\uc744 \uacf1\ud558\ub294 \uac83\uc740 **_\uc720\ud6a8\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4_**."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "it0P-AV0-Jb4"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default():\n",
        "  # Create a matrix (2-d tensor) with 3 rows and 4 columns.\n",
        "  x = tf.constant([[5, 2, 4, 3], [5, 1, 6, -2], [-1, 3, -1, -2]],\n",
        "                  dtype=tf.int32)\n",
        "\n",
        "  # Create a matrix with 4 rows and 2 columns.\n",
        "  y = tf.constant([[2, 2], [3, 5], [4, 5], [1, 6]], dtype=tf.int32)\n",
        "\n",
        "  # Multiply `x` by `y`. \n",
        "  # The resulting matrix will have 3 rows and 2 columns.\n",
        "  matrix_multiply_result = tf.matmul(x, y)\n",
        "\n",
        "  with tf.Session() as sess:\n",
        "    print(matrix_multiply_result.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "OVR8QPif-MeS",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ## \ud150\uc11c \ud615\ud0dc \ubcc0\uacbd\n",
        "\n",
        "\ud150\uc11c \ub367\uc148\uacfc \ud589\ub82c \uacf1\uc148\uc5d0\uc11c \uac01\uac01 \ud53c\uc5f0\uc0b0\uc790\uc5d0 \uc81c\uc57d\uc870\uac74\uc744 \ubd80\uc5ec\ud558\uba74\n",
        "\ud150\uc11c\ud50c\ub85c\uc6b0 \ud504\ub85c\uadf8\ub798\uba38\ub294 \uc790\uc8fc \ud150\uc11c\uc758 \ud615\ud0dc\ub97c \ubcc0\uacbd\ud574\uc57c \ud569\ub2c8\ub2e4. \n",
        "\n",
        "`tf.reshape` \uba54\uc11c\ub4dc\ub97c \uc0ac\uc6a9\ud558\uc5ec \ud150\uc11c\uc758 \ud615\ud0dc\ub97c \ubcc0\uacbd\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \n",
        "\uc608\ub97c \ub4e4\uc5b4 8x2 \ud150\uc11c\ub97c 2x8 \ud150\uc11c\ub098 4x4 \ud150\uc11c\ub85c \ud615\ud0dc\ub97c \ubcc0\uacbd\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "fziRnmuy-O9x"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default():\n",
        "  # Create an 8x2 matrix (2-D tensor).\n",
        "  matrix = tf.constant([[1,2], [3,4], [5,6], [7,8],\n",
        "                        [9,10], [11,12], [13, 14], [15,16]], dtype=tf.int32)\n",
        "\n",
        "  # Reshape the 8x2 matrix into a 2x8 matrix.\n",
        "  reshaped_2x8_matrix = tf.reshape(matrix, [2,8])\n",
        "  \n",
        "  # Reshape the 8x2 matrix into a 4x4 matrix\n",
        "  reshaped_4x4_matrix = tf.reshape(matrix, [4,4])\n",
        "\n",
        "  with tf.Session() as sess:\n",
        "    print(\"Original matrix (8x2):\")\n",
        "    print(matrix.eval())\n",
        "    print(\"Reshaped matrix (2x8):\")\n",
        "    print(reshaped_2x8_matrix.eval())\n",
        "    print(\"Reshaped matrix (4x4):\")\n",
        "    print(reshaped_4x4_matrix.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "L05ob6a_G77m",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " \n",
        "\ub610\ud55c `tf.reshape`\ub97c \uc0ac\uc6a9\ud558\uc5ec \ud150\uc11c\uc758 \ucc28\uc6d0 \uc218(\\'\uc21c\uc704\\')\ub97c \ubcc0\uacbd\ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\uc608\ub97c \ub4e4\uc5b4 8x2 \ud150\uc11c\ub97c 3-D 2x2x4 \ud150\uc11c\ub098 1-D 16-\uc694\uc18c \ud150\uc11c\ub85c \ubcc0\uacbd\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "b6cFa92YGyU5"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default():\n",
        "  # Create an 8x2 matrix (2-D tensor).\n",
        "  matrix = tf.constant([[1,2], [3,4], [5,6], [7,8],\n",
        "                        [9,10], [11,12], [13, 14], [15,16]], dtype=tf.int32)\n",
        "\n",
        "  # Reshape the 8x2 matrix into a 3-D 2x2x4 tensor.\n",
        "  reshaped_2x2x4_tensor = tf.reshape(matrix, [2,2,4])\n",
        "  \n",
        "  # Reshape the 8x2 matrix into a 1-D 16-element tensor.\n",
        "  one_dimensional_vector = tf.reshape(matrix, [16])\n",
        "\n",
        "  with tf.Session() as sess:\n",
        "    print(\"Original matrix (8x2):\")\n",
        "    print(matrix.eval())\n",
        "    print(\"Reshaped 3-D tensor (2x2x4):\")\n",
        "    print(reshaped_2x2x4_tensor.eval())\n",
        "    print(\"1-D vector:\")\n",
        "    print(one_dimensional_vector.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "3MpcwWj9-Sqp",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ### \uc2e4\uc2b5 #1: \ub450 \uac1c\uc758 \ud150\uc11c\ub97c \uacf1\ud558\uae30 \uc704\ud574 \ub450 \ud150\uc11c\uc758 \ud615\ud0dc\ub97c \ubcc0\uacbd\ud569\ub2c8\ub2e4.\n",
        "\n",
        "\ub2e4\uc74c \ub450 \ubca1\ud130\ub294 \ud589\ub82c \uacf1\uc148\uacfc \ud638\ud658\ub418\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "  *  `a = tf.constant([5, 3, 2, 7, 1, 4])`\n",
        "  *  `b = tf.constant([4, 6, 3])`\n",
        "\n",
        "\uc774 \ubca1\ud130\ub97c \ud589\ub82c \uacf1\uc148\uc5d0 \ud638\ud658\ub420 \uc218 \uc788\ub294 \ud53c\uc5f0\uc0b0\uc790\ub85c \ud615\ud0dc\ub97c \ubcc0\uacbd\ud558\uc138\uc694.\n",
        "\uadf8\ub7f0 \ub2e4\uc74c \ud615\ud0dc\uac00 \ubcc0\uacbd\ub41c \ud150\uc11c\uc5d0\uc11c \ud589\ub82c \uacf1\uc148 \uc791\uc5c5\uc744 \ud638\ucd9c\ud558\uc138\uc694."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "CrpowaWo-VLq"
      }
    },
    {
      "source": [
        "  # Write your code for Task 1 here."
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "p6idvaeK-Zxq",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ### \ud574\uacb0 \ubc29\ubc95\n",
        "\n",
        "\ud574\uacb0 \ubc29\ubc95\uc744 \ubcf4\ub824\uba74 \uc544\ub798\ub97c \ud074\ub9ad\ud558\uc138\uc694."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "EYzU56M4MG_x"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default(), tf.Session() as sess:\n",
        "  # Task: Reshape two tensors in order to multiply them\n",
        "  \n",
        "  # Here are the original operands, which are incompatible\n",
        "  # for matrix multiplication:\n",
        "  a = tf.constant([5, 3, 2, 7, 1, 4])\n",
        "  b = tf.constant([4, 6, 3])\n",
        "  # We need to reshape at least one of these operands so that\n",
        "  # the number of columns in the first operand equals the number\n",
        "  # of rows in the second operand.\n",
        "\n",
        "  # Reshape vector \"a\" into a 2-D 2x3 matrix:\n",
        "  reshaped_a = tf.reshape(a, [2,3])\n",
        "\n",
        "  # Reshape vector \"b\" into a 2-D 3x1 matrix:\n",
        "  reshaped_b = tf.reshape(b, [3,1])\n",
        "\n",
        "  # The number of columns in the first matrix now equals\n",
        "  # the number of rows in the second matrix. Therefore, you\n",
        "  # can matrix mutiply the two operands.\n",
        "  c = tf.matmul(reshaped_a, reshaped_b)\n",
        "  print(c.eval())\n",
        "\n",
        "  # An alternate approach: [6,1] x [1, 3] -> [6,3]"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "8Sef4d0SMMtk",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ## \ubcc0\uc218, \ucd08\uae30\ud654, \ud560\ub2f9\n",
        "\n",
        "\uc9c0\uae08\uae4c\uc9c0 \uc218\ud589\ud55c \ubaa8\ub4e0 \uc5f0\uc0b0\uc740 \uc815\uc801 \uac12(`tf.constant`)\uc5d0\uc11c \uc2e4\ud589\ub418\uc5c8\uace0; `eval()`\uc744 \ud638\ucd9c\ud558\uba74 \ud56d\uc0c1 \uac19\uc740 \uacb0\uacfc\uac00 \ubc18\ud658\ub418\uc5c8\uc2b5\ub2c8\ub2e4. \ud150\uc11c\ud50c\ub85c\uc6b0\uc5d0\uc11c\ub294 `\ubcc0\uc218` \uac1d\uccb4\ub97c \uc815\uc758\ud560 \uc218 \uc788\uc73c\uba70, \ubcc0\uc218 \uac12\uc740 \ubcc0\uacbd\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \n",
        "\n",
        "\ubcc0\uc218\ub97c \ub9cc\ub4e4 \ub54c \ucd08\uae30 \uac12\uc744 \uba85\uc2dc\uc801\uc73c\ub85c \uc124\uc815\ud558\uac70\ub098 \uc774\ub2c8\uc15c\ub77c\uc774\uc800(\uc608: \ubd84\ud3ec)\ub97c \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "x1JYo7iE2oKk"
      }
    },
    {
      "source": [
        "g = tf.Graph()\n",
        "with g.as_default():\n",
        "  # Create a variable with the initial value 3.\n",
        "  v = tf.Variable([3])\n",
        "\n",
        "  # Create a variable of shape [1], with a random initial value,\n",
        "  # sampled from a normal distribution with mean 1 and standard deviation 0.35.\n",
        "  w = tf.Variable(tf.random_normal([1], mean=1.0, stddev=0.35))"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "6opLnjfD3PdL",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " \ud150\uc11c\ud50c\ub85c\uc6b0\uc758 \ud55c \uac00\uc9c0 \ud2b9\uc9d5\uc740 **\ubcc0\uc218 \ucd08\uae30\ud654\uac00 \uc790\ub3d9\uc73c\ub85c \uc2e4\ud589\ub418\uc9c0 \uc54a\ub294\ub2e4**\ub294 \uac83\uc785\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4 \ub2e4\uc74c \ube14\ub85d\uc5d0\uc11c\ub294 \uc624\ub958\uac00 \ubc1c\uc0dd\ud569\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "qDYRXHTA4PTB"
      }
    },
    {
      "source": [
        "with g.as_default():\n",
        "  with tf.Session() as sess:\n",
        "    try:\n",
        "      v.eval()\n",
        "    except tf.errors.FailedPreconditionError as e:\n",
        "      print(\"Caught expected error: \", e)"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "d0OX1YRY5PTP",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " \ubcc0\uc218\ub97c \ucd08\uae30\ud654\ud558\ub294 \uac00\uc7a5 \uc26c\uc6b4 \ubc29\ubc95\uc740 `global_variables_initializer`\ub97c \ud638\ucd9c\ud558\ub294 \uac83\uc785\ub2c8\ub2e4. `eval()`\uacfc \uac70\uc758 \ube44\uc2b7\ud55c `Session.run()`\uc758 \uc0ac\uc6a9\uc744 \ucc38\uace0\ud558\uc138\uc694."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "v7_aT7Hr5rnC"
      }
    },
    {
      "source": [
        "with g.as_default():\n",
        "  with tf.Session() as sess:\n",
        "    initialization = tf.global_variables_initializer()\n",
        "    sess.run(initialization)\n",
        "    # Now, variables can be accessed normally, and have values assigned to them.\n",
        "    print(v.eval())\n",
        "    print(w.eval())\n"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "z2lvhrxI5zJF",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " \ucd08\uae30\ud654\ub41c \ubcc0\uc218\ub294 \uac19\uc740 \uc138\uc158 \ub0b4\uc5d0\uc11c\ub294 \uac12\uc744 \uc720\uc9c0\ud569\ub2c8\ub2e4. \ud558\uc9c0\ub9cc \uc0c8 \uc138\uc158\uc744 \uc2dc\uc791\ud558\uba74 \ub2e4\uc2dc \ucd08\uae30\ud654\ud574\uc57c \ud569\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "0GkYh7jf6JUd"
      }
    },
    {
      "source": [
        "with g.as_default():\n",
        "  with tf.Session() as sess:\n",
        "    sess.run(tf.global_variables_initializer())\n",
        "    # These three prints will print the same value.\n",
        "    print(w.eval())\n",
        "    print(w.eval())\n",
        "    print(w.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "_E8_lhS06IoV",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " \ubcc0\uc218 \uac12\uc744 \ubcc0\uacbd\ud558\ub824\uba74 `\ud560\ub2f9` \uc791\uc5c5\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4. `\ud560\ub2f9` \uc791\uc5c5\uc744 \ub9cc\ub4e4\uae30\ub9cc \ud558\uba74 \uc2e4\ud589\ub418\ub294 \uac83\uc740 \uc544\ub2d9\ub2c8\ub2e4. \ucd08\uae30\ud654\uc640 \ub9c8\ucc2c\uac00\uc9c0\ub85c \ud560\ub2f9 \uc791\uc5c5\uc744 `\uc2e4\ud589`\ud574\uc57c \ubcc0\uc218 \uac12\uc774 \uc5c5\ub370\uc774\ud2b8\ub429\ub2c8\ub2e4."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "yrZ31hPw66uy"
      }
    },
    {
      "source": [
        "with g.as_default():\n",
        "  with tf.Session() as sess:\n",
        "    sess.run(tf.global_variables_initializer())\n",
        "    # This should print the variable's initial value.\n",
        "    print(v.eval())\n",
        "\n",
        "    assignment = tf.assign(v, [7])\n",
        "    # The variable has not been changed yet!\n",
        "    print(v.eval())\n",
        "\n",
        "    # Execute the assignment op.\n",
        "    sess.run(assignment)\n",
        "    # Now the variable is updated.\n",
        "    print(v.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "zD0D1DCR7NBX",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " \ub85c\ub4dc \ubc0f \uc800\uc7a5\uacfc \uac19\uc774 \uc5ec\uae30\uc5d0\uc11c \ub2e4\ub8e8\uc9c0 \uc54a\uc740 \ubcc0\uc218\uc5d0 \uad00\ud55c \uc8fc\uc81c\ub3c4 \ub354 \ub9ce\uc774 \uc788\uc2b5\ub2c8\ub2e4. \uc790\uc138\ud788 \uc54c\uc544\ubcf4\ub824\uba74 [\ud150\uc11c\ud50c\ub85c\uc6b0 \ubb38\uc11c](https://www.tensorflow.org/programmers_guide/variables)\ub97c \ucc38\uc870\ud558\uc138\uc694."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "tB78Zq4h78Zr"
      }
    },
    {
      "source": [
        " ### \uc2e4\uc2b5 #2: \uc8fc\uc0ac\uc704 2\uac1c 10\ubc88 \uad74\ub9ac\uae30\ub97c \uc2dc\ubbac\ub808\uc774\uc158\ud569\ub2c8\ub2e4.\n",
        "\n",
        "\uc8fc\uc0ac\uc704 \uc2dc\ubbac\ub808\uc774\uc158\uc744 \ub9cc\ub4ed\ub2c8\ub2e4. \uc5ec\uae30\uc5d0\uc11c `10x3` 2-D \ud150\uc11c\ub97c \uc0dd\uc131\ud558\uba70 \uc870\uac74\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "  * \uc5f4 `1` \ubc0f `2`\ub294 \uac01\uac01 \uc8fc\uc0ac\uc704 1\uac1c\ub97c 1\ubc88 \ub358\uc84c\uc744 \ub54c\uc758 \uac12\uc785\ub2c8\ub2e4.\n",
        "  * \uc5f4 `3`\uc740 \uac19\uc740 \uc904\uc758 \uc5f4 `1`\uacfc `2`\uc758 \ud569\uc785\ub2c8\ub2e4.\n",
        "\n",
        "\uc608\ub97c \ub4e4\uc5b4 \uccab \ubc88\uc9f8 \ud589\uc758 \uac12\uc740 \ub2e4\uc74c\uacfc \uac19\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "  * \uc5f4 `1`\uc740 `4`\n",
        "  * \uc5f4 `2`\ub294 `3`\n",
        "  * \uc5f4 `3`\uc740 `7`\n",
        "\n",
        "[\ud150\uc11c\ud50c\ub85c\uc6b0 \ubb38\uc11c](https://www.tensorflow.org/api_guides/python/array_ops)\ub97c \ucc38\uc870\ud558\uc5ec \uc774 \ubb38\uc81c\ub97c \ud574\uacb0\ud574 \ubcf4\uc138\uc694."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "iFIOcnfz_Oqw"
      }
    },
    {
      "source": [
        "# Write your code for Task 2 here."
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "ocwT0iXH-nhT",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    },
    {
      "source": [
        " ### \ud574\uacb0 \ubc29\ubc95\n",
        "\n",
        "\ud574\uacb0 \ubc29\ubc95\uc744 \ubcf4\ub824\uba74 \uc544\ub798\ub97c \ud074\ub9ad\ud558\uc138\uc694."
      ],
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "Kt7aojXkR_qS"
      }
    },
    {
      "source": [
        "with tf.Graph().as_default(), tf.Session() as sess:\n",
        "  # Task 2: Simulate 10 throws of two dice. Store the results\n",
        "  # in a 10x3 matrix.\n",
        "\n",
        "  # We're going to place dice throws inside two separate\n",
        "  # 10x1 matrices. We could have placed dice throws inside\n",
        "  # a single 10x2 matrix, but adding different columns of\n",
        "  # the same matrix is tricky. We also could have placed\n",
        "  # dice throws inside two 1-D tensors (vectors); doing so\n",
        "  # would require transposing the result.\n",
        "  dice1 = tf.Variable(tf.random_uniform([10, 1],\n",
        "                                        minval=1, maxval=7,\n",
        "                                        dtype=tf.int32))\n",
        "  dice2 = tf.Variable(tf.random_uniform([10, 1],\n",
        "                                        minval=1, maxval=7,\n",
        "                                        dtype=tf.int32))\n",
        "\n",
        "  # We may add dice1 and dice2 since they share the same shape\n",
        "  # and size.\n",
        "  dice_sum = tf.add(dice1, dice2)\n",
        "\n",
        "  # We've got three separate 10x1 matrices. To produce a single\n",
        "  # 10x3 matrix, we'll concatenate them along dimension 1.\n",
        "  resulting_matrix = tf.concat(\n",
        "      values=[dice1, dice2, dice_sum], axis=1)\n",
        "\n",
        "  # The variables haven't been initialized within the graph yet,\n",
        "  # so let's remedy that.\n",
        "  sess.run(tf.global_variables_initializer())\n",
        "\n",
        "  print(resulting_matrix.eval())"
      ],
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "6UUluecQSCvr",
        "colab": {
          "autoexec": {
            "wait_interval": 0,
            "startup": false
          }
        }
      },
      "outputs": [],
      "execution_count": 0
    }
  ],
  "metadata": {
    "colab": {
      "default_view": {},
      "version": "0.3.2",
      "collapsed_sections": [
        "JndnmDMp66FL",
        "EYzU56M4MG_x",
        "Kt7aojXkR_qS"
      ],
      "name": "creating_and_manipulating_tensors.ipynb",
      "views": {}
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  }
}
